package com.sc.nft.dao;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.RoughStoneOrder;
import com.sc.nft.entity.dto.OrderDetailsDTO;
import com.sc.nft.entity.dto.RefundOrderPageDTO;
import com.sc.nft.entity.vo.RefundOrderPageVO;
import com.sc.nft.entity.vo.RoughStoneOrderVO;
import com.sc.nft.enums.ClassifyType;
import com.sc.nft.enums.OrderStatusEnum;
import com.sc.nft.mapper.RoughStoneOrderMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 原始订单表(sc_rough_stone_order)数据DAO
 *
 * @author zxy
 * @since 2023-08-14 16:40:43
 * @description 汇付相关
 */
@Slf4j
@Repository
public class RoughStoneOrderDao extends ServiceImpl<RoughStoneOrderMapper, RoughStoneOrder> {

    public RoughStoneOrder getByOrderNo(String orderNo) {
        return new LambdaQueryChainWrapper<RoughStoneOrder>(baseMapper).eq(RoughStoneOrder::getOrderNo, orderNo).one();

    }
    public Page<RoughStoneOrder> orderPage(Long userId, OrderStatusEnum orderStatus,   int pageNo, int pageSize) {
        if (orderStatus.getCode() == OrderStatusEnum.ALL.getCode()) {
            return new LambdaQueryChainWrapper<RoughStoneOrder>(baseMapper).eq(RoughStoneOrder::getUserId, userId).orderByDesc(RoughStoneOrder::getId).page(new Page<>(pageNo, pageSize));
        }
        return new LambdaQueryChainWrapper<RoughStoneOrder>(baseMapper).eq(RoughStoneOrder::getUserId, userId).eq(orderStatus != null, RoughStoneOrder::getStatus, orderStatus).orderByDesc(RoughStoneOrder::getId).page(new Page<>(pageNo, pageSize));
    }

    public boolean closeOrder(Long id) {
        LambdaUpdateWrapper<RoughStoneOrder> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(RoughStoneOrder::getStatus,OrderStatusEnum.CLOSE);
        wrapper.eq(RoughStoneOrder::getId,id);
        wrapper.eq(RoughStoneOrder::getStatus,OrderStatusEnum.WAIT_PAY);
        return update(wrapper);
    }

    public OrderDetailsDTO getOrderDetailsById(Long id) {
        return baseMapper.getOrderDetailsById(id);
    }

    public RoughStoneOrder getOrderByProductAndUserId(Long userId, Long productId) {
        LambdaQueryWrapper<RoughStoneOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(RoughStoneOrder::getStatus, OrderStatusEnum.WAIT_PAY);
        wrapper.eq(RoughStoneOrder::getUserId, userId);
        wrapper.eq(RoughStoneOrder::getProductId, productId);
        wrapper.last("limit 1");
        return getOne(wrapper);
    }

    public List<RoughStoneOrder> getCloseOrder() {
        LambdaQueryWrapper<RoughStoneOrder> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(RoughStoneOrder::getCloseTime, DateTime.now());
        wrapper.eq(RoughStoneOrder::getStatus, OrderStatusEnum.WAIT_PAY);
        return list(wrapper);

    }

    /**
     *  原石订单分页
     */
    public Page<RoughStoneOrderVO> orderPageFromAdmin(String orderNo, OrderStatusEnum status, ClassifyType typeClassify, Date startOrderTime, Date endOrderTime, Date startPayTime, Date endPayTime,String userTel,int pageNo, int pageSize) {
        return baseMapper.orderPageFromAdmin(orderNo,status,typeClassify,startOrderTime,endOrderTime,startPayTime,endPayTime,userTel,new Page(pageNo,pageSize));
    }

    /**
     *  获取所有原石订单数据
     */
    public List<RoughStoneOrderVO> roughStoneOrderExcel(String orderNo, OrderStatusEnum status, ClassifyType typeClassify, Date startOrderTime, Date endOrderTime, Date startPayTime, Date endPayTime,String userTel){
        return baseMapper.roughStoneOrderExcel(orderNo, status, typeClassify, startOrderTime, endOrderTime, startPayTime, endPayTime,userTel);
    }
    public IPage<RefundOrderPageVO> refundOrderPage(RefundOrderPageDTO request, Long userId) {
        List<RefundOrderPageVO>list=new ArrayList<>();
        LambdaQueryWrapper<RoughStoneOrder> wrapper = Wrappers.lambdaQuery();
        wrapper.select(RoughStoneOrder::getOrderNo,RoughStoneOrder::getFirstTitle,RoughStoneOrder::getProductPrice,RoughStoneOrder::getStatus,RoughStoneOrder::getCreateTime,RoughStoneOrder::getPayTime,RoughStoneOrder::getCount);
        wrapper.eq(RoughStoneOrder::getUserId,userId);
        wrapper.eq(request.getStatus()!=null,RoughStoneOrder::getStatus,request.getStatus());
        wrapper.eq(StrUtil.isNotEmpty(request.getOrderNo()), RoughStoneOrder::getOrderNo,request.getOrderNo());
        wrapper.orderByDesc(RoughStoneOrder::getId);
        Page page = page(request.toMp(), wrapper);
        List<RoughStoneOrder> records = page.getRecords();
        records.forEach(roughStoneOrder -> {
            RefundOrderPageVO refundOrderPageVO = new RefundOrderPageVO();
            refundOrderPageVO.setOrderNo(roughStoneOrder.getOrderNo());
            refundOrderPageVO.setFirstTitle(roughStoneOrder.getFirstTitle());
            refundOrderPageVO.setBuyPrice(roughStoneOrder.getProductPrice());
            refundOrderPageVO.setCount(roughStoneOrder.getCount());
            refundOrderPageVO.setStatus(roughStoneOrder.getStatus());
            refundOrderPageVO.setPayTime(roughStoneOrder.getPayTime());
            refundOrderPageVO.setCreateTime(roughStoneOrder.getCreateTime());
            list.add(refundOrderPageVO);
        });

        page.setRecords(list);
        return page;
    }
}